package fab.zjitc.net.androidhomeworktab3;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.view.Window;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTabLayout;
private ViewPager mViewPager;
private List<String> mTitles;
private List<Fragment> mFragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar=findViewById(R.id.tool);
        mTabLayout=findViewById(R.id.tabs);
        mViewPager=findViewById(R.id.vp);
        setSupportActionBar(mToolbar);
        init();
    }

    private void init() {
        mTitles=new ArrayList<String>();
        mTitles.add("红色");
        mTitles.add("黄色");
        mTitles.add("绿色");
        for (int i = 0; i <mTitles.size() ; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles.get(i)));
        }

        mFragments=new ArrayList<>();
        ListFragment fragment=new ListFragment();
        ListFragment2 fragment2=new ListFragment2();
        ListFragment3 fragment3=new ListFragment3();

        mFragments.add(fragment);
        mFragments.add(fragment2);
        mFragments.add(fragment3);

        FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),mTitles,mFragments);
        mViewPager.setAdapter(fragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(fragmentAdapter);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                Bitmap bitmap=null;
                if (i==0){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.dm1);
                }else if (i==1){
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.dm2);
                }else {
                    bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.dm3);
                }

                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(@Nullable Palette palette) {
                        Palette.Swatch vibrant=palette.getVibrantSwatch();
                        if (vibrant==null){
                            for (Palette.Swatch swatch:palette.getSwatches()){
                                vibrant=swatch;
                                break;
                            }
                        }
                        //这样获取的颜色可以进行改变
                        int rbg=vibrant.getRgb();

                        mTabLayout.setBackgroundColor(rbg);
                        mToolbar.setBackgroundColor(rbg);
                        if (Build.VERSION.SDK_INT>21){
                            Window window=getWindow();
                            //状态栏改变颜色
                            window.setStatusBarColor(rbg);
                        }
                    }
                });
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
